<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="关键字搜索" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入关键字(用户昵称手机号)" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column label="用户id" align="center" prop="id" />
      <el-table-column label="昵称" align="center" prop="nickName" />
            <el-table-column label="头像" align="center" prop="photo">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.photo" />
        </template>
      </el-table-column>

      <el-table-column label="真实姓名" align="center" prop="name" />
      <el-table-column label="年龄" align="center" prop="age" />

      <el-table-column label="im账号" align="center" prop="imUsername" />
      <el-table-column label="手机号" align="center" prop="phone" />
      
      <el-table-column label="背景图" align="center" prop="backgroundUrl">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.backgroundUrl" />
        </template>
      </el-table-column>

      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == 0 ? '女' : scope.row.identity == 1 ? '男' : scope.row.identity == 2 ? '未知' : '无'
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份" align="center" prop="identity">
        <template slot-scope="scope">
          <span>{{ scope.row.identity == 0 ? '普通用户' : scope.row.identity == 1 ? '主持' : scope.row.identity == 2 ? '代理' :
            '无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否认证" align="center" prop="isAuth">
        <template slot-scope="scope">
          <span style="color: cornflowerblue;">{{ scope.row.identity == 0 ? '未认证' : scope.row.identity == 1 ? '已认证' :
            '无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="个性签名" align="center" prop="personalityName" />
      <el-table-column label="兴趣爱好" align="center" prop="interestHobby" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="登录时间" align="center" prop="loginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注销时间" align="center" prop="logoutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.logoutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="照片墙" align="center" prop="photoWall" />
      <el-table-column label="0封号1正常2注销" align="center" prop="status" />
      <el-table-column label="身份证号码" align="center" prop="cardNum" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['user:user:edit']">用户信息管理</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['user:user:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="真实姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入真实姓名" disabled />
        </el-form-item>

        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="photo">
          <ImageUpload @input="uploadSuccess" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-input v-model="form.identity" placeholder="请输入0普通用户1主持2代理" />
        </el-form-item>
        <el-form-item label="是否实名认证" prop="isAuth">
          <el-input v-model="form.isAuth" placeholder="请输入0未认证1已认证" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="个性签名" prop="personalityName">
          <el-input v-model="form.personalityName" placeholder="请输入个性签名" />
        </el-form-item>
        <el-form-item label="兴趣爱好" prop="interestHobby">
          <el-input v-model="form.interestHobby" placeholder="请输入兴趣爱好" />
        </el-form-item>
        <el-form-item label="所在服务器" prop="placeServer">
          <el-input v-model="form.placeServer" placeholder="请输入所在服务器" />
        </el-form-item>

        <el-form-item label="身份证号码" prop="cardNum">
          <el-input v-model="form.cardNum" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="青少年模式开关" prop="isAdolescent">
          <el-input v-model="form.isAdolescent" placeholder="请输入青少年模式开关0关闭1开启" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/user/user";

import ImageUpload from "@/components/ImageUpload/index.vue"

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        page: 1,
        size: 10,
        search: '',
        startTime: '',
        endTiime: '',
        identity:'1'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        photo: [
          { required: true, message: "头像不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        imUsername: [
          { required: true, message: "im账号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        backgroundUrl: [
          { required: true, message: "背景图url不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "0女1男2未知不能为空", trigger: "change" }
        ],
        identity: [
          { required: true, message: "0普通用户1主持2代理不能为空", trigger: "blur" }
        ],
        isAuth: [
          { required: true, message: "0未认证1已认证不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省份不能为空", trigger: "blur" }
        ],
        personalityName: [
          { required: true, message: "个性签名不能为空", trigger: "blur" }
        ],
        interestHobby: [
          { required: true, message: "兴趣爱好不能为空", trigger: "blur" }
        ],
        placeServer: [
          { required: true, message: "所在服务器不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "0未删除1已删除不能为空", trigger: "blur" }
        ],
        photoWall: [
          { required: true, message: "照片墙不能为空", trigger: "blur" }
        ],

        status: [
          { required: true, message: "0封号1正常2注销不能为空", trigger: "change" }
        ],
        cardNum: [
          { required: true, message: "身份证号码不能为空", trigger: "blur" }
        ],
        isAdolescent: [
          { required: true, message: "青少年模式开关0关闭1开启不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    uploadSuccess(res) {
      this.form.photo = res;
      console.log("图片上传.....", res)
    },
    /** 查询房间列表 */
    getList() {
      if (this.dateRange) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTiime = this.dateRange[1];
      }


      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.data.data;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        photo: null,
        nickName: null,
        imUsername: null,
        phone: null,
        backgroundUrl: null,
        sex: null,
        identity: null,
        isAuth: null,
        province: null,
        personalityName: null,
        interestHobby: null,
        placeServer: null,
        isDel: null,
        remark: null,
        updateTime: null,
        createTime: null,
        loginTime: null,
        logoutTime: null,
        photoWall: null,
        salt: null,
        password: null,
        openId: null,
        status: null,
        cardNum: null,
        isAdolescent: null,
        tutelageWord: null,
        tutelageSalt: null,
        rongToken: null,
        roomNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房间编号为"' + ids + '"的数据项？').then(function () {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
